//
//  KKEmoticonPopView.swift
//  EmotionKeyBoard
//
//  Created by King.Kong. on 16/6/3.
//  Copyright © 2016年 King.Kong. All rights reserved.
//

import UIKit

class KKEmoticonPopView: UIView {

    @IBOutlet weak var emoticonBtn: KKEmoticonButton!

    class func loadPopView() -> KKEmoticonPopView {
        let pop = NSBundle.mainBundle().loadNibNamed("KKEmoticonPopView", owner: nil, options: nil).last as! KKEmoticonPopView
        return pop
    }

    
    func show(btn: KKEmoticonButton) {
        let window = UIApplication.sharedApplication().windows.last!
        if self.superview == nil {
            //添加到window
            window.addSubview(self)
        }
        self.hidden = false
        
        //转换坐标系
        // convertRect(_转换的对象的frame或者bounds_, toView: __转换到哪一个视图__
        //frame 还是bounds 需要根据调用这个方法的对象是视图本身还是视图的父视图
        let rect = btn.superview!.convertRect(btn.frame, toView: window)
        //设置pop的frame
        
        center.x = CGRectGetMidX(rect)
        frame.origin.y = CGRectGetMaxY(rect) - bounds.height
        emoticonBtn.emoticon = btn.emoticon
    }
    
    func dismissAfter() {
        let time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(Double(NSEC_PER_SEC) * 0.2))
        dispatch_after(time_t, dispatch_get_main_queue()) {
            self.hidden = true
        }
    }

}
